package com.share.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.domain.Cabinet;
import com.share.service.CabinetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("cabinet")
public class CabinetController {

    @Autowired
    private CabinetService cabinetService;

    // 分页 + 模糊查询
    @GetMapping("list")
    public Page<Cabinet> list(@RequestParam(required = false) String name,
                              @RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<Cabinet> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Cabinet::getName, name);
        }
        return cabinetService.page(new Page<>(pageNum, pageSize), wrapper);
    }

    // 根据 ID 查询
    @GetMapping("{id}")
    public Cabinet getById(@PathVariable BigInteger id) {
        return cabinetService.getById(id);
    }

    // 新增
    @PostMapping
    public boolean create(@RequestBody Cabinet cabinet) {
        return cabinetService.save(cabinet);
    }

    // 修改
    @PutMapping
    public boolean update(@RequestBody Cabinet cabinet) {
        return cabinetService.updateById(cabinet);
    }

    // 删除
    @DeleteMapping("{id}")
    public boolean delete(@PathVariable BigInteger id) {
        return cabinetService.removeById(id);
    }
}
